{extend name="admininfo/admininfo" /}
{block name='main'}
<h2 style="color:#2aabd2">{$name}列表</h2>

<form action="{:url('admin/Classify/addclassify')}" method='post'>
	<input type="hidden" value='{$table}' name='table' id='tablename'>
	<input type="hidden" value='{$name}' name='name'>
	<input type="submit" class="btn btn-default btn-success" value='添加分类'>
</form>
<!-- <a href="{:url('admin/Classify/addclassify',['table',$table])}" class="btn btn-default btn-success">添加分类</a> -->
<table class="table table-hover table-bordered" style="text-align: center;" id="test">

	<tr>
	    <th style="text-align: center;">
	        id
	    </th>
	    <th style="text-align: center;">
	        name
	    </th>
	    <th style="text-align: center;">
	        操作
	    </th>
	</tr>

	{foreach $list as $v}

	<tr id = "{$v['id']}">
	    <td>
	        {$v['id']}
	    </td>
	    <td>
			{$v['name']}
	    </td>
	    <td >
	        <div class="row clearfix">
	            <!-- <div class="col-md-12 column" > -->
					<div class="col-md-4">
	               		 <button type="button" class="btn btn-default btn-success bit"  date_id="{$v['id']}" >查看子分类</button>
						
					</div>
					<div class="col-md-4">						
		                <button class="btn btn-default btn-success add" date_id="{$v['id']}" data-toggle="modal" data-target="#myModal" >
		                	添加子分类
		                </button>
					</div>


	                <div class="col-md-4">
	                	<form action="{:url('admin/Classify/del')}" method='post' >
							<input type="hidden" name='id' value="{$v['id']}">

							<input type="hidden" name='table' value="{$table}">
	                		<button type="submit" class="btn btn-default btn-danger" id='button'>删除分类</button>               	
	                	</form>
	                	
	                </div>
	            <!-- </div> -->
	        </div>
			
	    </td>

	</tr>

	
	{/foreach}
</table>


<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h4 class="modal-title" id="myModalLabel">
				
				</h4>
			</div>
			<div class="modal-body" style='padding-left:200px;padding-bottom:50px'>
				
			<form class="form-horizontal">
				<div >
						<div class="control-group">
							 <label class="control-label" for="inputEmail">父级ID</label>
							<div class="controls">
								<input id="inputEmail" type="text" value='' readonly/>
							</div>
						</div>
						<div class="control-group">
							 <label class="control-label" for="inputPassword">名字</label>
							<div class="controls">
								<input id="inputPassword" type="text" value=''/>
							</div>
						</div>
				
					
				</div>
	
			
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭
				</button>
				<button class="btn btn-primary adds">
					保存
				</button>
			</form>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
	<script>
		$(function(){
				var table = $('#tablename').val();
			$('.bit').click(function(){
				
				var date_id = $(this).attr('date_id');
				// console.log(table,date_id);
				$.ajax({
					type:'post',
					url: '/admin/Classify/subcategory',
					data:{ 'id':date_id ,'table':table},
					dataType:'json',
					success:function(data){
						for (var i = 0; i < data.length; i++) {
							var id = data[i].id;
							var name = data[i].name;

							var button = "<button type='button' s_id= "+id+" class='btn btn-default btn-danger sbit' id='sbit'>删除分类</button>"
							// var button = " <button type='button' class='btn btn-default btn-danger' id='button'>编辑分类</button>"

							var trHTML = "<tr style='background-color:#ccc;' id= "+id+"><td>"+id+"</td><td>"+name+"</td><td>"+button+"</td></tr>";
							// console.log($('#button').innerHTML());
							$("#"+date_id).after(trHTML);
						}
						
						// console.log(data);
					}

				})			
				
			})
			//增加子分类
			$('.add').click(function(){
				var date_id = $(this).attr('date_id');
				// console.log(date_id);
				inputEmail.value = date_id;
			})

			$(document).on('click', '.adds', function(e){
		
				var name = $('#inputPassword').val();
				var parent_id = $("#inputEmail").val();
				
				// console.log(name);
				$.ajax({
					type:'post',
					url: '/admin/Classify/addsubcategory',
					data:{ 'name': name,'parent_id':parent_id ,'table':table},
					dataType:'json',
					success:function(data){
						console.log(data);
						if (data) {
							alert('添加成功')
						}else{
							alert('添加失败')
						}
					}

				})	
				
			})


			//删除
			$(document).on('click', '.sbit', function(e) {
  
 				var s_id = $(this).attr('s_id');
 				// console.log(s_id);
 				$.ajax({
 					type:'post',
 					url:  '/admin/Classify/delsubcategory',
 					data:{ 'id': s_id ,'table':table},
 					dataType:'json',
 					success:function(data){
 						// console.log($(this).parents('tr').remove());
 				
 						console.log(data);
 						
 						console.log($('#'+s_id).remove());

 						
 					}

 				})
  
			});
			

		})
	</script>

{/block}